package com.tsd.base.service.impl;

import com.tsd.base.dao.BaseExpressOrgRateMapper;
import com.tsd.base.entity.BaseExpressOrgExt;
import com.tsd.base.entity.BaseExpressOrgItemExt;
import com.tsd.base.entity.BaseExpressOrgRate;
import com.tsd.base.entity.BaseExpressOrgRateExt;
import com.tsd.base.service.BaseExpressOrgRateService;
import com.tsd.common.vo.BatchDeleteLogicVo;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUserExt;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 描述：BaseExpressOrgRate 服务实现层
 *
 * @author Hillpool
 * @date 2021/04/15 11:17:56
 */
@Service
public class BaseExpressOrgRateServiceImpl extends BaseServiceImpl implements BaseExpressOrgRateService {

    @Resource
    private BaseExpressOrgRateMapper baseExpressOrgRateMapper;

    @Override
    public BaseExpressOrgRate getBaseExpressOrgRateById(Long id) throws Exception {
        return baseExpressOrgRateMapper.selectByPrimaryKey(id);
    }

    @Override
    public BaseExpressOrgRateExt queryBySid(String sid) throws Exception {
        return baseExpressOrgRateMapper.selectBySid(sid);
    }

    @Override
    public void deleteBaseExpressOrgRate(Long id, SysUserExt opUser) throws Exception {
        BaseExpressOrgRate old = baseExpressOrgRateMapper.selectByPrimaryKey(id);
        super.checkEmpty(old, "该记录不存在");
        super.autoInjectBaseData(old, opUser, this.TYPE_DELETE);
        baseExpressOrgRateMapper.updateByPrimaryKeySelective(old);
    }

    @Override
    public void saveBaseExpressOrgRate(BaseExpressOrgRateExt record, SysUserExt opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            baseExpressOrgRateMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            baseExpressOrgRateMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public void saveBaseExpressOrgRate(BaseExpressOrgExt record, SysUserExt opUser) throws Exception {
        List<BaseExpressOrgRateExt> oldExtList = record.getRate_list();
        if (oldExtList == null) {
            oldExtList = new ArrayList<>();
        }
        List<BaseExpressOrgRateExt> rateExtList = new ArrayList<>();
        for (BaseExpressOrgRateExt rateExt : oldExtList) {
            if (rateExt.getWeight() == null && rateExt.getFreight() == null) {
                //如果都为空，则跳过
                continue;
            } else {
                super.checkEmpty(rateExt.getWeight(), "重量不能为空");
                super.checkEmpty(rateExt.getFreight(), "运费不能为空");
                if (rateExt.getWeight() <= 0) {
                    throw new HlpException("重量不能小于或等于0");
                }
                if (rateExt.getFreight() <= 0) {
                    throw new HlpException("运费不能小于或等于0");
                }
                rateExtList.add(rateExt);
            }
        }

        boolean hasRateMode = false;
        if (record.getItem_list() != null) {
            for (BaseExpressOrgItemExt t : record.getItem_list()) {
                if (BaseExpressOrgItemExt.MODE_RATE.equalsIgnoreCase(t.getValuation_mode())) {
                    hasRateMode = true;
                    break;
                }
            }
        }
        if (hasRateMode && rateExtList.isEmpty()) {
            throw new HlpException("设置了计价模式为“查费率表”的规则，但费率表为空");
        }
        if (HlpUtils.isEmptyList(rateExtList)) {
            return;
        }
        List<Long> existIdList = ListUtil.filterMap(rateExtList, t -> !HlpUtils.isEmpty(t.getId()), BaseExpressOrgRateExt::getId);
        BaseExpressOrgRateExt params = new BaseExpressOrgRateExt();
        params.setDr(0);
        params.setExpress_sid(record.getSid());
        List<BaseExpressOrgRateExt> oldList = baseExpressOrgRateMapper.selectByEntity(params);
        List<Long> delIds = new ArrayList<>();
        if (!HlpUtils.isEmptyList(oldList)) {
            delIds = ListUtil.filterMap(oldList, t -> !existIdList.contains(t.getId()), BaseExpressOrgRateExt::getId);
        }
        if (!HlpUtils.isEmptyList(delIds)) {
            BatchDeleteLogicVo logicVo = new BatchDeleteLogicVo(opUser);
            logicVo.setIdList(delIds);
            baseExpressOrgRateMapper.deleteLogicByIdList(logicVo);
        }
        List<BaseExpressOrgRateExt> insertList = new ArrayList<>();
        List<BaseExpressOrgRateExt> updateList = new ArrayList<>();
        for (BaseExpressOrgRateExt rateExt : rateExtList) {
            rateExt.setExpress_sid(record.getSid());
            if (HlpUtils.isEmpty(rateExt.getId())) {
                super.autoInjectBaseData(rateExt, opUser, this.TYPE_CREATE);
                insertList.add(rateExt);
            } else {
                super.autoInjectBaseData(rateExt, opUser, this.TYPE_UPDATE);
                updateList.add(rateExt);
            }
            if (insertList.size() == 100) {
                baseExpressOrgRateMapper.insertList(insertList);
                insertList.clear();
            }
            if (updateList.size() == 100) {
                baseExpressOrgRateMapper.updateList(updateList);
                updateList.clear();
            }
        }
        if (!HlpUtils.isEmptyList(insertList)) {
            baseExpressOrgRateMapper.insertList(insertList);
        }
        if (!HlpUtils.isEmptyList(updateList)) {
            baseExpressOrgRateMapper.updateList(updateList);
        }
    }

    @Override
    public List<BaseExpressOrgRateExt> findBaseExpressOrgRates(Map<String, Object> params, PageBean page) throws Exception {
        return baseExpressOrgRateMapper.selectByPage(params, page);
    }

    @Override
    public List<BaseExpressOrgRateExt> findBaseExpressOrgRates(Map<String, Object> params) throws Exception {
        return baseExpressOrgRateMapper.selectByParams(params);
    }

    @Override
    public List<BaseExpressOrgRateExt> queryByEntity(BaseExpressOrgRateExt params) throws Exception {
        return baseExpressOrgRateMapper.selectByEntity(params);
    }
}
